/*
 *  Copyright (C) 2010 Glauco
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.balisunrise.swing.transaction;

import java.util.ArrayList;
import java.util.List;
import javax.swing.JTable;
import org.balisunrise.common.GridModel;
import org.balisunrise.common.GridSelectionListener;
import org.balisunrise.common.VisualEntityType;
import org.balisunrise.common.abstracts.AbstractVisualEntity;
import org.balisunrise.swing.components.util.ListSelectionListenerImpl;

/**
 *
 * @author Glauco
 */
public class BaliGrid extends AbstractVisualEntity
        implements org.balisunrise.common.Grid<JTable>{

    private GridModel gridModel;
    private List<GridSelectionListener> listeners;
    private JTable visualRepresentation;

    private ListSelectionListenerImpl selectionListener;

    public BaliGrid() {
        listeners = new ArrayList<GridSelectionListener>();
        getSelectionListener().setGridSelectionListeners(listeners);
    }

    public void setGridModel(GridModel gridModel) {
        this.gridModel = gridModel;
    }

    public void setVisualRepresentation(JTable visualRepresentation) {
        getSelectionListener().setTable(visualRepresentation);
        this.visualRepresentation = visualRepresentation;
    }

    private ListSelectionListenerImpl getSelectionListener() {
        if (selectionListener == null)
            selectionListener = new ListSelectionListenerImpl();
        return selectionListener;
    }

    public GridModel getGridModel() {
        return gridModel;
    }

    public void addSelectionListener(GridSelectionListener listener) {
        listeners.add(listener);
    }

    public void removeSelectionListener(int index) {
        try{
            listeners.remove(index);
        }catch (IndexOutOfBoundsException ioob){
            System.out.println("Falha ao remover listner do BaliGrid:\n" +
                    ioob.getMessage());
        }
    }

    public void removeSelectionListener(GridSelectionListener listener) {
        try{
            listeners.remove(listener);
        }catch(Exception ex){
            System.out.println(ex.getMessage());
        }
    }

    public GridSelectionListener[] getSelectionListeners() {
        return listeners.toArray(new GridSelectionListener[]{});
    }

    public JTable getVisualRepresentation() {
        return visualRepresentation;
    }

    /**
     * Retorna o tipo do VisualEntity.<BR>
     * Sempre retornará GRID.
     * @return VisualEntityType.GRID
     */
    public VisualEntityType getVisualEntityType() {
        return VisualEntityType.GRID;
    }

    public String getName() {
        return getVisualRepresentation().getName();
    }
}
